package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.R;
import com.atguigu.commonutils.utils.JwtUtils;
import com.atguigu.eduservice.client.OrderClient;
import com.atguigu.eduservice.entity.*;
import com.atguigu.eduservice.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/courseapi")
@Api(description = "课程前台接口")
//@CrossOrigin
public class CourseApiController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private OrderClient orderClient;

    @ApiOperation("查询分页数据")
    @PostMapping("map/{curret}/{size}")
    public R getMap(@ApiParam(value = "当前页",name = "curret",required = true)@PathVariable long curret,
                    @ApiParam(value = "每页数",name = "size",required = true)@PathVariable long size)
    {
        Page<EduCourse> page = new Page<>(curret,size);
        QueryWrapper<EduCourse> query = new QueryWrapper<>();
        query.orderByDesc("id");
        courseService.page(page,query);
        long total = page.getTotal();
        List<EduCourse> list = page.getRecords();
        long pages = page.getPages();
        long sizes = page.getSize();
        boolean hasNext = page.hasNext();
        boolean hsaPrevious = page.hasPrevious();
        long currents = page.getCurrent();
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",list);
        map.put("pages",pages);
        map.put("size",sizes);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hsaPrevious);
        map.put("current",currents);
        return R.ok().data("map",map);
    }

    @ApiOperation("根据id查询")
    @GetMapping("selectById/{id}")
    public R selectById(@PathVariable String id, HttpServletRequest request){
        EduCourse eduCourse = courseService.getById(id);
        //查询课程描述
        EduCourseDescription courseDescription = courseDescriptionService.getById(id);
        //根据id查询所有章节节
        QueryWrapper<EduChapter> query = new QueryWrapper<>();
        query.eq("course_id",id);
        List<EduChapter> list = chapterService.list(query);
        //根据课程id查询所有的小节
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",id);
        List<EduVideo> videoList = videoService.list(queryWrapper);
        //根据讲师id查询讲师信息
        EduTeacher teacher = teacherService.getById(eduCourse.getTeacherId());
        String s =JwtUtils.getMemberIdByJwtToken(request);
        boolean isBuy = orderClient.isBuyCourse(JwtUtils.getMemberIdByJwtToken(request),id);
        return R.ok().data("eduCourse",eduCourse).data("list",list).data("courseDescription",courseDescription).data("videoList",videoList).data("teacher",teacher).data("isBuy",isBuy);
    }
}
